<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
    <link rel="stylesheet" th:href="@{/pear/admin/css/other/common.css}"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <script id="uEditor" name="content" type="text/plain" style="width:100%;height:400px;"></script>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-success layui-btn" lay-submit=""
                    lay-filter="email-edit" id="email-edit">
                <i class="layui-icon layui-icon-ok"></i>
                保存
            </button>
            <button type="reset" class="layui-btn layui-btn" id="email-reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:src="@{/pear/component/ueditor/ueditor.config.js}"></script>
<script th:src="@{/pear/component/ueditor/ueditor.all.js}"></script>
<script th:src="@{/pear/component/ueditor/lang/zh-cn/zh-cn.js}"></script>
<script th:inline="javascript">
    layui.use(function () {
        let baseURL = /*[[@{/}]]*/ '';

        let $ = layui.jquery;
        let layer = layui.layer;
        let form = layui.form;
        let util = layui.util;
        let ue = UE.getEditor('uEditor', {
            serverUrl: baseURL + 'cms/ueditor/upload',
            autoFloatEnabled: false,  // 解决工具栏粘顶问题
        });
        $.ajax({
            url: baseURL + "cms/email/getEmailById?id=" + parent.id,
            type: 'GET',
            success: function (res) {
                console.log(res.data);
                const decodedData = atob(res.data.content);
                let c = new TextDecoder('utf-8').decode(new Uint8Array([...decodedData].map(char => char.charCodeAt(0))));
                ue.setContent(c);
                form.on('submit(email-edit)', function (data) {
                    let loading = layer.msg('邮件保存中', {icon: 16, shade: 0.3, time: 0});
                    data.field.id = parent.id
                    res.data.content = btoa(unescape(encodeURIComponent(data.field.content)));
                    $.ajax({
                        url: baseURL + 'cms/email/update',
                        data: JSON.stringify(res.data),
                        dataType: 'json',
                        contentType: 'application/json',
                        type: 'post',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code === 1000) {
                                layer.msg('更新成功，正在重新发送邮件...', {
                                    icon: 1,
                                    time: 2000
                                }, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                                    $.ajax({
                                        url: baseURL + 'cms/email/resend',
                                        data: {
                                            id: res.data.id,
                                            sendTime: util.toDateString(Date.parse(new Date()), 'yyyy-MM-dd HH:mm:ss')
                                        },
                                        type: 'post',
                                        success: function (result) {
                                            layer.close(loading);
                                            if (result.code === 1000) {
                                                layer.msg("发送成功，重试次数还剩" + remainingTries + "次", {
                                                    icon: 1,
                                                    time: 2000
                                                })
                                                parent.layui.table.reload("emailLog-table");

                                            } else {
                                                layer.msg("发送失败，重试次数还剩" + remainingTries + "次", {
                                                    icon: 2,
                                                    time: 2000
                                                });
                                            }
                                        },
                                        error: function () {
                                            layer.close(loading);
                                            layer.msg("发送失败", {
                                                icon: 2,
                                                time: 2000
                                            });
                                        }
                                    })
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 2000
                                });
                            }
                        },
                        error: function () {
                            layer.close(loading);
                            layer.msg('服务器错误，请稍后再试！', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    });
                    return false;
                });
            }
        })
    })
</script>
</body>
</html>